package com.aviccxzx.smarthome.UserModule;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aviccxzx.smarthome.R;
import com.aviccxzx.smarthome.base.GosUserModuleBaseActivity;
import com.aviccxzx.smarthome.entity.FeedBack;
import com.aviccxzx.smarthome.entity.UserInfo;
import com.aviccxzx.smarthome.netutil.app.App;
import com.aviccxzx.smarthome.service.RetrofitHelper;
import com.aviccxzx.smarthome.service.RetrofitService;
import com.aviccxzx.smarthome.utils.ChangeNameDialogUtil;
import com.aviccxzx.smarthome.utils.PermissionTool;
import com.aviccxzx.smarthome.widget.HeadImageView;
import com.smarttop.library.bean.City;
import com.smarttop.library.bean.County;
import com.smarttop.library.bean.Province;
import com.smarttop.library.bean.Street;
import com.smarttop.library.db.manager.AddressDictManager;
import com.smarttop.library.utils.LogUtil;
import com.smarttop.library.widget.AddressSelector;
import com.smarttop.library.widget.BottomDialog;
import com.smarttop.library.widget.OnAddressSelectedListener;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.jovision.AppConsts.APP_PATH;


public class MyCenterdetail extends GosUserModuleBaseActivity implements EasyPermissions.PermissionCallbacks, View.OnClickListener, OnAddressSelectedListener, AddressSelector.OnDialogCloseListener {
    private Dialog dialog;
    //    private PopupWindow popupWindow;
    View parentView;
    String path;
    HeadImageView head_img;
    String filePath = APP_PATH + "/head.png";

    AddressDictManager addressDictManager;
    BottomDialog bottomdialog;
    private String provinceCode;
    private String cityCode;
    private String countyCode;
    private String streetCode;
    TextView address_tv, regist_phone_tv, name_tv;
    String imgPath = "";
    String uid = "";
    String phone = "";
    LinearLayout layout;
    AlertDialog alertDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setContentView(R.layout.detail_center_v19);
        } else {

            //            setContentView(R.layout.detail_center);
        }
        ((TextView) findViewById(R.id.title_tv)).setText("个人详情");
        parentView = LayoutInflater.from(MyCenterdetail.this).inflate(
                R.layout.detail_center_v19, null);
        setview();
        //        getHeadImg();
        getInfo();
        AddressSelector selector = new AddressSelector(this);
        //获取地址管理数据库
        addressDictManager = selector.getAddressDictManager();
        selector.setTextSize(14);//设置字体的大小
        selector.setIndicatorBackgroundColor(android.R.color.holo_orange_light);//设置指示器的颜色
        selector.setTextSelectedColor(android.R.color.holo_orange_light);//设置字体获得焦点的颜色
        selector.setTextUnSelectedColor(android.R.color.holo_blue_light);//设置字体没有获得焦点的颜色
        phone = spf.getString("UserName", "");
        if (!TextUtils.isEmpty(spf.getString("UserName", ""))) {
            regist_phone_tv.setText(phone);
        }
        findViewById(R.id.choose_headImg_ll).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                layout = (LinearLayout) LayoutInflater.from(MyCenterdetail.this).inflate(R.layout.mh_info_crapimg, null);
                alertDialog = new AlertDialog.Builder(MyCenterdetail.this).create();

                Button fromGallery = (Button) layout.findViewById(R.id.fromCallery);
                fromGallery.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        alertDialog.dismiss();
                        fileTask();
                    }
                });
                Button fromCamera = (Button) layout.findViewById(R.id.fromCamera);
                fromCamera.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //                choicePhoto(false);
                        alertDialog.dismiss();

                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                            cameraTask();
                        } else {
                            if (PermissionTool.isCameraCanUse()) {
                                openCamera();
                            } else {
                                // 当前APP没有摄像头权限弹层，或者其他相关提示
                                new AppSettingsDialog.Builder(MyCenterdetail.this, MyCenterdetail.this.getResources().getString(R.string.camera_show))
                                        .setTitle("权限申请")
                                        .setPositiveButton("确认")
                                        .setNegativeButton("取消", null /* click listener */)
                                        .setRequestCode(REQUEST_CAMERA_PERM)
                                        .build()
                                        .show();
                            }
                        }
                    }
                });
                Button cancel = (Button) layout.findViewById(R.id.cancel);
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        alertDialog.dismiss();
                    }
                });

                alertDialog.show();

                alertDialog.setContentView(layout);
                Window window = alertDialog.getWindow();
                ViewGroup.LayoutParams layoutParams = window.getAttributes();
                WindowManager manager = window.getWindowManager();
                Display display = manager.getDefaultDisplay();
                layoutParams.width = (int) (display.getWidth() * 0.9);
                layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
                window.setAttributes((WindowManager.LayoutParams) layoutParams);

            }
        });
    }

    private void setview() {
        uid = spf.getString("Uid", "");
        address_tv = (TextView) findViewById(R.id.address_tv);
        regist_phone_tv = (TextView) findViewById(R.id.regist_phone_tv);
        name_tv = (TextView) findViewById(R.id.name_tv);
        head_img = (HeadImageView) findViewById(R.id.head_img);
        TextView rightTv = (TextView) findViewById(R.id.title_right_text);
        rightTv.setVisibility(View.VISIBLE);
        rightTv.setText("保存");
        findViewById(R.id.title_right_text).setOnClickListener(this);
        findViewById(R.id.ll_title_left_view).setOnClickListener(this);
        findViewById(R.id.choose_headImg_ll).setOnClickListener(this);
        findViewById(R.id.nickname_ll).setOnClickListener(this);
        findViewById(R.id.change_phone_ll).setOnClickListener(this);
        findViewById(R.id.address_ll).setOnClickListener(this);

    }


    public void getInfo() {
        RetrofitService service = RetrofitHelper.getInstance(this).getServer();
        Call<UserInfo> call = service.getinfo(uid);
        call.enqueue(new Callback<UserInfo>() {
            @Override
            public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
                if (response.body() != null && response.body().getData() != null) {
                    if (response.body().getData().getUname() != null) {
                        name_tv.setText(response.body().getData().getUname().toString());
                    }
                    if (response.body().getData().getRegion() != null && response.body().getData().getCity() != null) {
                        region = response.body().getData().getRegion().toString();
                        cityName = response.body().getData().getCity().toString();
                        address_tv.setText(response.body().getData().getRegion().toString() + response.body().getData().getCity().toString());
                    }
                }
            }

            @Override
            public void onFailure(Call<UserInfo> call, Throwable t) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_title_left_view:
                finish();
                break;
            case R.id.title_right_text:
                update();
                break;
            case R.id.choose_headImg_ll:
                //点击上传头像
                break;
            case R.id.nickname_ll:
                //修改昵称
                changeName();
                break;
            case R.id.change_phone_ll:
                //修改手机号
                break;
            case R.id.address_ll:
                //set address
                if (bottomdialog != null) {
                    bottomdialog.show();
                } else {
                    bottomdialog = new BottomDialog(this);
                    bottomdialog.setOnAddressSelectedListener(this);
                    bottomdialog.setDialogDismisListener(this);
                    bottomdialog.setTextSize(14);//设置字体的大小
                    bottomdialog.setIndicatorBackgroundColor(R.color.title_common_color);//设置指示器的颜色
                    bottomdialog.setTextSelectedColor(R.color.title_common_color);//设置字体获得焦点的颜色
                    bottomdialog.setTextUnSelectedColor(android.R.color.black);//设置字体没有获得焦点的颜色
                    bottomdialog.show();
                }
                break;
        }

    }

    public void update() {
        if (!TextUtils.isEmpty(region) && !TextUtils.isEmpty(cityName)/*&&!TextUtils.isEmpty(name_tv.getText().toString())*/) {
            progressDialog.show();
            RetrofitService service = RetrofitHelper.getInstance(MyCenterdetail.this).getServer();
            Call<FeedBack> call = service.update(name_tv.getText().toString(), region, cityName, phone);
            call.enqueue(new Callback<FeedBack>() {
                @Override
                public void onResponse(Call<FeedBack> call, Response<FeedBack> response) {
                    progressDialog.dismiss();
                    spf.edit().putString("selfname", name_tv.getText().toString()).commit();
                    App.getApp().showToast("修改成功");
                }

                @Override
                public void onFailure(Call<FeedBack> call, Throwable t) {
                    progressDialog.dismiss();
                    App.getApp().showToast("修改失败" + "....." + t.getMessage());
                }
            });
        } else {
            showToast("请选择地址信息后再保存");
        }
    }


    private void changeName() {
        new ChangeNameDialogUtil(MyCenterdetail.this, "修改姓名", new ChangeNameDialogUtil.SureInterfance() {
            @Override
            public void sureTodo(final String name) {
                name_tv.setText(name);
            }
        }).showSelfDialog();
    }

    private void changePhone() {
        new ChangeNameDialogUtil(MyCenterdetail.this, "修改手机号", new ChangeNameDialogUtil.SureInterfance() {
            @Override
            public void sureTodo(String name) {
                //确定修改 处理方案
                App.getApp().showToast(name);
            }
        }).showSelfDialog();

    }


    /**
     * 请求CAMERA权限码
     */
    public static final int REQUEST_CAMERA_PERM = 101;
    /**
     * 请求操作文件的权限码
     */
    public static final int REQUEST_FILE_PERM = 102;

    /**
     * EsayPermissions接管权限处理逻辑
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        // Forward results to EasyPermissions
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @AfterPermissionGranted(REQUEST_CAMERA_PERM)
    public void cameraTask() {
        if (EasyPermissions.hasPermissions(MyCenterdetail.this, Manifest.permission.CAMERA)) {
            // Have permission, do the thing!
            if (PermissionTool.isCameraCanUse()) {
                openCamera();
            } else {
                // 当前APP没有摄像头权限弹层，或者其他相关提示
                new AppSettingsDialog.Builder(MyCenterdetail.this, MyCenterdetail.this.getResources().getString(R.string.camera_show))
                        .setTitle("权限申请")
                        .setPositiveButton("确认")
                        .setNegativeButton("取消", null /* click listener */)
                        .setRequestCode(REQUEST_CAMERA_PERM)
                        .build()
                        .show();
            }
        } else {
            // Ask for one permission
            EasyPermissions.requestPermissions(this, "需要请求拍照权限",
                    REQUEST_CAMERA_PERM, Manifest.permission.CAMERA);
        }
    }

    @AfterPermissionGranted(REQUEST_FILE_PERM)
    public void fileTask() {
        if (EasyPermissions.hasPermissions(MyCenterdetail.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            // Have permission, do the thing!
            openPics();
        } else {
            // Ask for one permission
            EasyPermissions.requestPermissions(this, "需要请求操作文件夹的权限",
                    REQUEST_FILE_PERM, Manifest.permission.WRITE_EXTERNAL_STORAGE);
        }
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        switch (requestCode) {
            case REQUEST_CAMERA_PERM:
                openCamera();
                break;
            case REQUEST_FILE_PERM:
                openPics();
                break;
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        switch (requestCode) {
            case REQUEST_CAMERA_PERM:
                if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
                    new AppSettingsDialog.Builder(this, MyCenterdetail.this.getResources().getString(R.string.camera_show))
                            .setTitle("权限申请")
                            .setPositiveButton("确认")
                            .setNegativeButton("取消", null /* click listener */)
                            .setRequestCode(REQUEST_CAMERA_PERM)
                            .build()
                            .show();
                }
                break;
            case REQUEST_FILE_PERM:
                if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
                    new AppSettingsDialog.Builder(this, "当前App需要操作文件夹的权限,需要打开设置页面么?")
                            .setTitle("权限申请")
                            .setPositiveButton("确认")
                            .setNegativeButton("取消", null /* click listener */)
                            .setRequestCode(REQUEST_FILE_PERM)
                            .build()
                            .show();
                }
                break;
        }

    }

    public void openPics() {
        //打开手机的图库;
        /*Intent intent = new Intent();
        intent.setType("image*//*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, 0x1);*/
        Intent intent1 = new Intent(Intent.ACTION_PICK, null);
        intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        startActivityForResult(intent1, 0x1);
    }

    String photoName;

    public void openCamera() {
        /*Intent intent = new Intent();
        //MediaStore.ACTION_IMAGE_CAPTURE  调用系统的照相机
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 0x3);*/
        Intent intent2 = new Intent("android.media.action.IMAGE_CAPTURE");
        photoName = "Pic_" + System.currentTimeMillis() + ".png";
        intent2.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
        intent2.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0);
        startActivityForResult(intent2, 0x2);//
    }

    /**
     * 调用系统的裁剪
     *
     * @param uri
     */
    public void cropPhoto(Uri uri) {
        try {
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(uri, "image/*");
            intent.putExtra("crop", "true");
            // aspectX aspectY 是宽高的比例
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            // outputX outputY 是裁剪图片宽高
            intent.putExtra("outputX", 100);
            intent.putExtra("outputY", 100);
            intent.putExtra("return-data", true);
            startActivityForResult(intent, 0x3);
        } catch (Exception e) {
            showToast("截图错误，请重试");
            e.printStackTrace();
        }
    }

    private Uri getImageUri() {
        return Uri.fromFile(new File(Environment.getExternalStorageDirectory(), photoName));
    }

    private void saveimg(final Bitmap mBitmap) {
        try {
            File file = new File(filePath);
            //如果文件不存在，则创建文件
            if (!file.exists()) {
                file.createNewFile();
            }
            //输出流
            FileOutputStream out = new FileOutputStream(file);
            /** mBitmap.compress 压缩图片
             *
             *  Bitmap.CompressFormat.PNG   图片的格式
             *   100  图片的质量（0-100）
             *   out  文件输出流
             */
            mBitmap.compress(Bitmap.CompressFormat.PNG, 70, out);
            out.flush();
            out.close();

            if (file.exists()) {
                progressDialog.show();
                // 创建 RequestBody，用于封装构建RequestBody
                RequestBody requestFile =
                        RequestBody.create(MediaType.parse("multipart/form-data"), file);

                // MultipartBody.Part  和后端约定好Key，这里的partName是用image
                MultipartBody.Part body =
                        MultipartBody.Part.createFormData("img", file.getName(), requestFile);
                String uid = spf.getString("Uid", "");
                RetrofitService service = RetrofitHelper.getInstance(this).getServer();
                Call<ResponseBody> call = service.uploadImage(uid, body);
                call.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        progressDialog.dismiss();
                        Log.i("upload", response.toString());
                        spf.edit().putString("headimg", imgPath).commit();
                        head_img.setImageBitmap(mBitmap);
                        showToast("头像修改成功");
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        progressDialog.dismiss();
                        showToast("头像修改失败，请重试");
                    }
                });

            }
        } catch (Exception e) {
            showToast("截图错误，请重试");
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }
        switch (requestCode) {
            case 0x3:
                try {
                    if (resultCode == Activity.RESULT_OK & data != null) {
                        Bundle extras = data.getExtras();
                        Bitmap head = extras.getParcelable("data");
                        if (head != null) {// 上传服务器代码
                            head_img.setImageBitmap(head);
//                            saveImg(head);
                            saveimg(head);
                            //                            uploadImage(head);
                        } else
                            showToast("截图错误，请重试");
                    }
                } catch (Exception e) {
                    showToast("截图错误，请重试");
                    e.printStackTrace();
                }
                break;
            case 0x1:// back from albem
                try {
                    if (resultCode == Activity.RESULT_OK) {
                        cropPhoto(data.getData());// 裁剪图片
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case 0x2:// back from camera
                try {
                    if (resultCode == Activity.RESULT_OK) {
                        cropPhoto(getImageUri());
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
        /*if (requestCode == 0x1) {
            if (data != null) {
                Uri uri = data.getData();
                cutImg(uri);
            }  else {
                return;
            }
        }
        if (requestCode == 0x2) {
            if (data != null) {
                Bundle bundle = data.getExtras();
                //得到图片
                Bitmap bitmap = bundle.getParcelable("data");
                //保存到图片到本地
                saveImg(bitmap);
                //设置图片
                //                head_img.setImageBitmap(bitmap);
                uploadImage(bitmap);
            } else {
                return;
            }
        }
        if (requestCode == 0x3) {
            if (data != null) {
                Bundle bundle = data.getExtras();
                if (bundle == null) {
                    return;
                }
                Bitmap bitmap = bundle.getParcelable("data");
                //保存到图片到本地
                saveImg(bitmap);
                //                head_img.setImageBitmap(bitmap);
                uploadImage(bitmap);
            } else {
                return;
            }
        }

        super.onActivityResult(requestCode, resultCode, data);*/
    }

    public void uploadImage(final Bitmap bitmap) {
        if (!TextUtils.isEmpty(imgPath)) {
            //            showToast(imgPath);
            //                Bitmap bitmap = BitmapFactory.decodeFile(path);
            //处理图片
            //TODO
            //                showToast(path);
            final File file = new File(imgPath);
            if (file.exists()) {
                progressDialog.show();
                // 创建 RequestBody，用于封装构建RequestBody
                RequestBody requestFile =
                        RequestBody.create(MediaType.parse("multipart/form-data"), file);

                // MultipartBody.Part  和后端约定好Key，这里的partName是用image
                MultipartBody.Part body =
                        MultipartBody.Part.createFormData("img", file.getName(), requestFile);
                String uid = spf.getString("Uid", "");
                RetrofitService service = RetrofitHelper.getInstance(this).getServer();
                Call<ResponseBody> call = service.uploadImage(uid, body);
                call.enqueue(new Callback<ResponseBody>() {
                    @Override
                    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                        progressDialog.dismiss();
                        Log.i("upload", response.toString());
                        spf.edit().putString("headimg", imgPath).commit();
                        head_img.setImageBitmap(bitmap);
                    }

                    @Override
                    public void onFailure(Call<ResponseBody> call, Throwable t) {
                        progressDialog.dismiss();
                        showToast("上传失败");
                    }
                });

            }
        }
    }


    //保存图片到本地，下次直接读取
    private void saveImg(Bitmap mBitmap) {
        File f = new File(filePath);
        try {
            //如果文件不存在，则创建文件
            if (!f.exists()) {
                f.createNewFile();
            }
            //输出流
            FileOutputStream out = new FileOutputStream(f);
            /** mBitmap.compress 压缩图片
             *
             *  Bitmap.CompressFormat.PNG   图片的格式
             *   100  图片的质量（0-100）
             *   out  文件输出流
             */
            mBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
            //            Toast.makeText(this, f.getAbsolutePath().toString(), Toast.LENGTH_SHORT).show();
            imgPath = f.getAbsolutePath().toString();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private void getImg(Uri uri) {
        try {
            InputStream inputStream = getContentResolver().openInputStream(uri);
            //从输入流中解码位图
            // Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
            //保存位图
            // img.setImageBitmap(bitmap);
            cutImg(uri);
            //关闭流
            inputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //裁剪图片
    private void cutImg(Uri uri) {
        if (uri != null) {
            Intent intent = new Intent("com.android.camera.action.CROP");
            intent.setDataAndType(uri, "image/*");
            //true:出现裁剪的框
            intent.putExtra("crop", "true");
            //裁剪宽高时的比例
            intent.putExtra("aspectX", 1);
            intent.putExtra("aspectY", 1);
            //裁剪后的图片的大小
            intent.putExtra("outputX", 300);
            intent.putExtra("outputY", 300);
            intent.putExtra("return-data", true);  // 返回数据
            //            intent.putExtra("output", uri);
            //            intent.putExtra("scale", true);
            startActivityForResult(intent, 0x2);
        } else {
            return;
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
        RetrofitService service = RetrofitHelper.getInstance(MyCenterdetail.this).getServer();
        Call<ResponseBody> call = service.getImage();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                if (response.body().byteStream() != null) {
                    Bitmap bitmap = BitmapFactory.decodeStream(response.body().byteStream());
                    if (bitmap != null) {
                        head_img.setBackgroundResource(0);
                        head_img.setImageBitmap(bitmap);
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });

    }

    @Override
    public void dialogclose() {
        if (bottomdialog != null) {
            bottomdialog.dismiss();
        }
    }

    String region = "";
    String cityName = "";

    @Override
    public void onAddressSelected(Province province, City city, County county, Street street) {
        provinceCode = (province == null ? "" : province.code);
        cityCode = (city == null ? "" : city.code);
        countyCode = (county == null ? "" : county.code);
        streetCode = (street == null ? "" : street.code);
        LogUtil.d("数据", "省份id=" + provinceCode);
        LogUtil.d("数据", "城市id=" + cityCode);
        LogUtil.d("数据", "乡镇id=" + countyCode);
        LogUtil.d("数据", "街道id=" + streetCode);
        final String s = (province == null ? "" : province.name) + " " + (city == null ? "" : city.name) + " " + (county == null ? "" : county.name) + " " +
                (street == null ? "" : street.name);

        if (bottomdialog != null) {
            bottomdialog.dismiss();
        }
        region = (province == null ? "" : province.name);
        cityName = (city == null ? "" : city.name) + " " + (county == null ? "" : county.name) + " " +
                (street == null ? "" : street.name);

        address_tv.setText(s);
        /*progressDialog.show();
        RetrofitService service = RetrofitHelper.getInstance(this).getServer();
        Call<FeedBack> call = service.update(uid, userName, region, cityName);
        call.enqueue(new Callback<FeedBack>() {
            @Override
            public void onResponse(Call<FeedBack> call, Response<FeedBack> response) {
                progressDialog.dismiss();
                App.getApp().showToast("修改成功");
                address_tv.setText(s);
            }

            @Override
            public void onFailure(Call<FeedBack> call, Throwable t) {
                progressDialog.dismiss();
                App.getApp().showToast("修改失败" + "....." + t.getMessage());
            }
        });*/

    }
}
